package com.example.app2024.canvas;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PathMeasure;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.view.animation.OvershootInterpolator;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.app2024.R;

public class SplashVfiew extends View
{
      private Paint mPaint;
      private Paint mHolePaint;
      private ValueAnimator mValueAnimator;

      private int mBackgroundColor = Color.WHITE;
      private int[] mCircleColors;

      private float mCenterX;
      private float mCenterY;

      private float mDistance;

      private float mCircleRadius = 18;
      private float mRotateRadius = 90;

      private float mCurrentRotateAngle = 0f;

      private float mCurrentRotateRadius = mRotateRadius;
      private float mCurrentHoleRadius = 0f;
      private int mRotateDuration = 1200;


      public SplashVfiew(Context context)
      {
            super(context);
      }

      public SplashVfiew(Context context, @Nullable AttributeSet attrs)
      {
            super(context, attrs);
            init(context);
      }

      private void init(Context context)
      {
            mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

            mHolePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
            mHolePaint.setStyle(Paint.Style.STROKE);
            mHolePaint.setColor(mBackgroundColor);

            mCircleColors = context.getResources().getIntArray(R.array.splash_circle_colors);
      }

      @Override
      protected void onSizeChanged(int w, int h, int oldw, int oldh)
      {
            super.onSizeChanged(w, h, oldw, oldh);
            mCenterX = w * 1f / 2;
            mCenterY = h * 1f / 2;
            mDistance = (float) Math.hypot(w, h) / 2;  //斜对角线长度 除 2
      }

      @Override
      protected void onDraw(@NonNull Canvas canvas)
      {
            super.onDraw(canvas);
            // 旋转
            // 扩散聚合
            // 水波纹
            if (mState == null)
            {
                  mState = new RotateState();

            }
            mState.drawState(canvas);
      }

      private SplashState mState;

      private abstract class SplashState
      {
            abstract void drawState(Canvas canvas);
      }

      private class RotateState extends SplashState
      {
            private RotateState()
            {
                  mValueAnimator = ValueAnimator.ofFloat(0, (float) (Math.PI * 2));
                  mValueAnimator.setRepeatCount(2);
                  mValueAnimator.setDuration(mRotateDuration);
                  mValueAnimator.setInterpolator(new LinearInterpolator());
                  mValueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener()
                  {
                        @Override
                        public void onAnimationUpdate(@NonNull ValueAnimator animation)
                        {
                              mCurrentRotateAngle = (float) animation.getAnimatedValue();
                              invalidate();
                        }
                  });
                  mValueAnimator.addListener(new AnimatorListenerAdapter()
                  {
                        @Override
                        public void onAnimationEnd(Animator animation)
                        {
                              super.onAnimationEnd(animation);
                              mState = new MerginState();
                        }
                  });
                  mValueAnimator.start();
            }

            @Override
            void drawState(Canvas canvas)  //画6个小球与背景
            {
                  drawBackground(canvas);
                  drawCircles(canvas);
            }
      }

      private void drawCircles(Canvas canvas)
      {
            float rotateAngle = (float) (Math.PI * 2 / mCircleColors.length);
            for (int i = 0; i < mCircleColors.length; i++)
            {
                  float angle = i * rotateAngle + mCurrentRotateAngle;
                  float cx = (float) (Math.cos(angle) * mCurrentRotateRadius + mCenterX);
                  float cy = (float) (Math.sin(angle) * mCurrentRotateRadius + mCenterY);
                  mPaint.setColor(mCircleColors[i]);
                  canvas.drawCircle(cx, cy, mCircleRadius, mPaint);
            }
      }

      /**
       * 三种状态共用的绘背景方法
       */
      private void drawBackground(Canvas canvas)
      {
            if (mCurrentHoleRadius > 0)
            {
                  float strokeWidth = mDistance - mCurrentHoleRadius;
                  float radius = strokeWidth / 2 + mCurrentHoleRadius;
                  Log.e("TAG",  "线宽是  " + strokeWidth + "  半径是   " + radius );
                  mHolePaint.setStrokeWidth(strokeWidth);
                  canvas.drawCircle(mCenterX, mCenterY, radius, mHolePaint);
            }
            else
            {
                  canvas.drawColor(mBackgroundColor);
            }
            PathMeasure pathMeasure;
      }

      private class MerginState extends SplashState
      {
            private MerginState()
            {
                  mValueAnimator = ValueAnimator.ofFloat(mCircleRadius, mRotateRadius);
                  // mValueAnimator.setRepeatCount(2);
                  mValueAnimator.setDuration(mRotateDuration);
                  mValueAnimator.setInterpolator(new OvershootInterpolator(10f));
                  mValueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener()
                  {
                        @Override
                        public void onAnimationUpdate(@NonNull ValueAnimator animation)
                        {
                              mCurrentRotateRadius = (float) animation.getAnimatedValue();
                              invalidate();
                        }
                  });
                  mValueAnimator.addListener(new AnimatorListenerAdapter()
                  {
                        @Override
                        public void onAnimationEnd(Animator animation)
                        {
                              super.onAnimationEnd(animation);
                              mState = new ExpandState();
                        }
                  });
                  mValueAnimator.reverse();
            }

            @Override
            void drawState(Canvas canvas)
            {
                  drawBackground(canvas);
                  drawCircles(canvas);
            }
      }

      private class ExpandState extends SplashState
      {
            public ExpandState()
            {
                  mValueAnimator = ValueAnimator.ofFloat(mCircleRadius, mDistance);
                  // mValueAnimator.setRepeatCount(2);
                  mValueAnimator.setDuration(mRotateDuration);
                  mValueAnimator.setInterpolator(new LinearInterpolator());
                  mValueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener()
                  {
                        @Override
                        public void onAnimationUpdate(@NonNull ValueAnimator animation)
                        {
                              mCurrentHoleRadius = (float) animation.getAnimatedValue();
                              invalidate();
                        }
                  });
                  mValueAnimator.start();
            }


            @Override
            void drawState(Canvas canvas)
            {
                  drawBackground(canvas);
            }
      }
}
